﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ManagerViewStaff : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Role"]!=null&&"Staff".Equals(Session["Role"]) == false)
        {
            Response.Redirect("~/Pages/HomePage/Home.aspx");
        }
        if (!IsPostBack)
        {
            
        }
    }

    private void GetAllStudent()
    {
        StaffBUS staff = new StaffBUS();
        grvStudents.DataSource = staff.getAllStudent().Tables[0];
        grvStudents.DataBind();
    }
    protected void btnSeachStudent_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "StaffViewStudent", "displayBlockStaff('StaffViewStudent');", true);
        StaffBUS staff = new StaffBUS();
        string name = txtSeachStudent.Text;
        grvStudents.DataSource = staff.searchStudentByName(name).Tables[0];
        grvStudents.DataBind();
    }
    protected void grvStudents_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvStudents.PageIndex = e.NewPageIndex;
        GetAllStudent();
    }
    protected void grvViewCompetition_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Convert.ToInt32(grvViewCompetition.DataKeys[e.RowIndex].Value.ToString());
        ManagerBUS dt = new ManagerBUS();
        if (dt.delCompetition(id))
        {
            grvViewCompetition.EditIndex = -1;
            Response.Write("<script>alert('One Competiton deleted')</script>");
        }
    }
    protected void btnSearchViewCompetition_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "StaffViewCompetition", "displayBlockStaff('StaffViewCompetition');", true);
        ManagerBUS manager = new ManagerBUS();
        string name = txtSearchViewCompetition.Text;
        grvViewCompetition.DataSource = manager.searchCompetition(name).Tables[0];
        grvViewCompetition.DataBind();
    }
    protected void btnChangePass_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "StaffChangePass", "displayBlockStaff('StaffChangePass');", true);
        string currentPass = txtCurrentPass.Text;
        StaffBUS staff = new StaffBUS();
        DataTable dt = staff.staffLogin(Session["username"].ToString(), currentPass).Tables[0];
        if (!((dt.Rows.Count) > 0))
        {
            lblWarningChangePass.Text = "CurrentPass not correct";
            return;
        }
        string newPass = txtNewPass.Text;
        string confirmPass = txtConfirmPass.Text;
        if (!(newPass.Equals(confirmPass)))
        {
            lblWarningChangePass.Text = "ConfirmPass not correct";
            return;
        }
        Staff s = new Staff();
        s.Id = Convert.ToInt32(dt.Rows[0][0].ToString());
        s.Username = Session["username"].ToString();
        s.Password = newPass;
        s.Fullname = dt.Rows[0][3].ToString();
        s.Address = dt.Rows[0][4].ToString();
        s.Email = dt.Rows[0][5].ToString();
        s.PhoneNumber = dt.Rows[0][6].ToString();
        s.Major = dt.Rows[0][7].ToString();
        ManagerBUS manager = new ManagerBUS();
        if (manager.updateStaff(s))
        {
            Response.Redirect("~/Pages/HomePage/Success.aspx");
        }
        else
        {
            lblWarningChangePass.Text = "Failed";
        }
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "StaffUpdateInfo", "displayBlockStaff('StaffUpdateInfo');", true);
        ManagerBUS manager = new ManagerBUS();
        Staff s = new Staff();
        s.Id = Convert.ToInt32(Session["Id"].ToString());
        s.Username = Session["username"].ToString();
        DataTable dt = manager.searchStaff(s).Tables[0];
        s.Password = dt.Rows[0][2].ToString();
        s.Fullname = txtFullNameUpdate.Text;
        s.Address = txtAddressUpdate.Text;
        s.Email = txtEmailUpdate.Text;
        s.PhoneNumber = txtPhoneUpdate.Text;
        s.Major = txtMajorUpdate.Text;
        if (manager.updateStaff(s))
        {
            Response.Redirect("~/Pages/HomePage/Success.aspx");
        }
        else
        {
            lblWarningUpdateInfo.Text = "Failed";
        }
    }
    protected void grvCompetition_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
            e.Row.Attributes.Add("style", "cursor:pointer;");
            e.Row.Attributes.Add("onclick", "location='viewDesign.aspx?cId=" + e.Row.Cells[0].Text + "'");
        }
    }
    protected void btnDisplayAllCompetition_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "StaffViewAllCompetition", "displayBlockStaff('StaffViewAllCompetition');", true);
        getAllCompetitionsByStaff();
    }

    private void getAllCompetitionsByStaff()
    {
        StaffBUS staff = new StaffBUS();
        DataSet ds = staff.getCompetitionAccess(Convert.ToInt32(Session["Id"].ToString()));
        if (ds == null)
        {
            Response.Write("<script>alert('No Competition')</script>");
        }
        else
        {
            grvCompetition.DataSource = ds.Tables[0];
            grvCompetition.DataBind();
        }
    }

    protected void btnSeachCompetition_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "StaffViewAllCompetition", "displayBlockStaff('StaffViewAllCompetition');", true);
        ManagerBUS m = new ManagerBUS();
        string name = txtSeachCompetition.Text;
        if ("".Equals(name))
        {
            getAllCompetitionsByStaff();
        }
        else
        {
            grvCompetition.DataSource = m.searchCompetition(name).Tables[0];
            grvCompetition.DataBind();
        }
    }
    protected void btnDisplayAllStudent_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "StaffViewStudent", "displayBlockStaff('StaffViewStudent');", true);
        getAllStudent();
    }

    private void getAllStudent()
    {
        StaffBUS staff = new StaffBUS();
        grvStudents.DataSource = staff.getAllStudent().Tables[0];
        grvStudents.DataBind();
    }
    protected void btnViewAllCompetition_Click(object sender, EventArgs e)
    {
        ScriptManager.RegisterStartupScript(this, GetType(), "StaffViewCompetition", "displayBlockStaff('StaffViewCompetition');", true);
        StaffBUS staff = new StaffBUS();
        grvViewCompetition.DataSource = staff.getCompetitionAccess(Convert.ToInt32(Session["Id"].ToString()));
        grvViewCompetition.DataBind();
    }
    protected void grvViewCompetition_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#ceedfc'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=''");
            e.Row.Attributes.Add("style", "cursor:pointer;");
            e.Row.Attributes.Add("onclick", "location='StaffUpdateCompetition.aspx?cId=" + e.Row.Cells[1].Text + "'");
        }
    }
}